package cn.dgut.training.controller;

import cn.dgut.training.annotation.LoginUser;
import cn.dgut.training.dto.GroupBuyingDTO;
import cn.dgut.training.entity.activity.GroupBuying;
import cn.dgut.training.service.IGroupBuyingService;
import cn.dgut.training.vo.BaseVO;
import cn.dgut.training.vo.CommonResponse;
import cn.dgut.training.vo.GroupBuyingVO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/group-buying")
public class GroupBuyingController {

    @Resource
    private IGroupBuyingService groupBuyingService;

    //    创建团购活动
    @PostMapping("/create")
    @Transactional
    public CommonResponse createActivity(@RequestBody @Valid GroupBuyingDTO groupBuyingDTO){
        GroupBuyingVO groupBuyingVO = groupBuyingService.createActivity(groupBuyingDTO);
        BaseVO<GroupBuyingVO> baseVO = new BaseVO<>();
        List<GroupBuyingVO> GroupBuyingVOList = new ArrayList<>();
        GroupBuyingVOList.add(groupBuyingVO);
        baseVO.setList(GroupBuyingVOList);
        return CommonResponse.success("成功");
    }

    //    修改团购活动
    @PostMapping("/update")
    @Transactional
    public Integer iUpdateActivity(@RequestParam("id") Long id){
        return groupBuyingService.updateActivity(id);
    }

    //    分页查询全部内容
    @PostMapping("/list")
    public CommonResponse listAllActivityByPage(@RequestBody @Valid GroupBuyingDTO groupBuyingDTO){
        return CommonResponse.success(groupBuyingService.listAllActivityByPage(groupBuyingDTO));
    }

    //    手动下架团购活动
    @PostMapping("/delete")
    @Transactional
    public CommonResponse deleteActivity(@RequestBody @Valid GroupBuyingDTO groupBuyingDTO){
        GroupBuyingVO groupBuyingVO = groupBuyingService.deleteActivity(groupBuyingDTO);
        BaseVO<GroupBuyingVO> baseVO = new BaseVO<>();
        List<GroupBuyingVO> GroupBuyingVOList = new ArrayList<>();
        GroupBuyingVOList.add(groupBuyingVO);
        baseVO.setList(GroupBuyingVOList);
        return CommonResponse.success(baseVO);
    }

    //用户查询参与的团购活动
    @PostMapping("/findUserJoin")
    @Transactional
    public CommonResponse findUserJoinGroup(@LoginUser Long userId,@RequestBody @Valid GroupBuyingDTO groupBuyingDTO){
        return CommonResponse.success(groupBuyingService.findUserJoinGroup(userId,groupBuyingDTO));
    }

    @GetMapping("iGetById")
    public GroupBuying iGetById(@RequestParam("grouponLinkId") Long grouponLinkId){
        return groupBuyingService.getById(grouponLinkId);
    }
}
